package com.cckp.week01;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 14546
 */
public class MyClassLoader extends ClassLoader {

    public static void main(String[] args) throws Exception {
        Object hello = new MyClassLoader().findClass("Hello").newInstance();
        Method m = hello.getClass().getMethod("hello");
        m.invoke(hello);
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        this.getResourceAsStream("file/Hello.xlass");
        try (FileInputStream fis = new FileInputStream("file\\Hello.xlass");) {
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            for (int i = 0; i < bytes.length; i++) {
                bytes[i]= (byte) (255-bytes[i]);
            }
            return defineClass(name,bytes,0,bytes.length);
        } catch (IOException e) {
            e.printStackTrace();
        }


        return super.findClass(name);
    }
}
